package com.pe.pgn.clubpgn.webapp.controller.categoria;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;

import com.pe.pgn.clubpgn.common.CLPConstantes;
import com.pe.pgn.clubpgn.domain.ClpbCategoria;
import com.pe.pgn.clubpgn.service.CategoriaService;
import com.pe.pgn.clubpgn.webapp.controller.BaseFormController;

@Controller
@RequestMapping("/categoria/**")
public class CategoriaController extends BaseFormController {

	@Autowired
	private CategoriaService categoriaService;
	
	private static final String ES_EDITABLE = "esEditable";
	
	@RequestMapping(value = "/categorialist.html")
    public void listarCategorias(Model model) {
		
		List<ClpbCategoria> categorias = categoriaService.obtenerCategorias();
		model.addAttribute("categorias", categorias);
	}
	
	@RequestMapping(value = "/categoriaform.html", method = RequestMethod.GET)
	public @ModelAttribute("categoria") ClpbCategoria verCategoria(
				@RequestParam(value = "id", required = false) Long id, Model model) {

		ClpbCategoria categoria = new ClpbCategoria();
		if (id != null) {			
			
			categoria = categoriaService.obtenerCategoria(id);			
			boolean categoriaConDependencias = categoriaService.esCategoriaConDependencias(id);			
			if(categoriaConDependencias){
				model.addAttribute(ES_EDITABLE, Boolean.FALSE);	
			}
			else{
				model.addAttribute(ES_EDITABLE, Boolean.TRUE);	
			}					
			return categoria;
		}
		
		model.addAttribute(ES_EDITABLE, Boolean.TRUE);
		categoria.setStCategoria(Boolean.TRUE);
		return categoria;
	}
	
	@RequestMapping(value = "/categoriaform.html", method = RequestMethod.POST)
	public String guardarCategoria(@ModelAttribute("categoria") ClpbCategoria categoria,
			BindingResult result, SessionStatus status, HttpServletRequest request, Model model) {
		
		try{
			
			boolean esCodigoRepetido = categoriaService.esCodigoCategoriaRepetido(categoria);
			if(esCodigoRepetido){
				model.addAttribute("categoria", categoria);
				saveError(request, getText("categoriaform.error.codigo.duplicado", request.getLocale()));
				return "/categoria/categoriaform";
			}
			
			if (result.hasErrors()) {
				model.addAttribute("categoria", categoria);
				return "/categoria/categoriaform";
			}
			
			categoriaService.guardarCategoria(categoria);
			saveMessage(request, getText("categoriaform.added", categoria.getDeCategoria().length() > CLPConstantes.LIMITE_CADENA ?
					categoria.getDeCategoria().substring(0, CLPConstantes.LIMITE_CADENA)
					.concat(CLPConstantes.PUNTOS_SUSPENSIVOS) : categoria.getDeCategoria(), 
					request.getLocale()));
			return "redirect:categorialist.html";
			
		}catch (Exception e) {
			model.addAttribute("categoria", categoria);
			saveError(request, getText("errors.general", request.getLocale()));
			return "/categoria/categoriaform";
		}		
	}
	
	@RequestMapping(value = "/eliminarcategoria.html")
	public String eliminarCategoria(@RequestParam(value = "id", required = false) Long id, 
			HttpServletRequest request, Model model) {
		
		try {			
			
			boolean categoriaConDependencias = categoriaService.esCategoriaConDependencias(id);
			if(categoriaConDependencias){
				ClpbCategoria categoria = categoriaService.obtenerCategoria(id);
				model.addAttribute("categoria", categoria);
				saveError(request, getText("categoriaform.error.categoria.dependendencias", request.getLocale()));
				return "/categoria/categoriaform";
			}			
			
			categoriaService.eliminarCategoria(id);
			saveMessage(request, getText("categoriaform.deleted", request.getLocale()));
			return "redirect:categorialist.html";
			
		} catch (Exception e) {
			ClpbCategoria categoria = categoriaService.obtenerCategoria(id);
			model.addAttribute("categoria", categoria);
			saveError(request, getText("errors.general", request.getLocale()));
			return "/categoria/categoriaform";
		}
	}
	
}
